#include "bank.hpp"

Bank::Bank(QObject *parent) :
    QObject(parent)
{
}


Bank::Bank(const Bank &other)
{
    QMapIterator<int, Register*> it(other.getRegisterMap());
    while(it.hasNext())
    {
	it.next();
	this->registerMap.insert(it.key(), new Register(*it.value()));
    }
}


Bank::~Bank()
{
    QMapIterator<int, Register*> it(this->registerMap);
    while(it.hasNext())
	delete it.value();
    this->registerMap.clear();
}


Register *Bank::getRegister(int address)
{
    if(registerMap.contains(address))
    {
	if(address == 0)
	{
	    return registerMap.value(registerMap.value(4)->getValue());
	}
	return registerMap.value(address);
    }
    return new Register(-1010101, -1010101);
}

void Bank::addRegister(int address, Register *reg)
{
    if(!registerMap.contains(address))
    {
	registerMap.insert(address, reg);
    }
}


const QMap<int, Register*> &Bank::getRegisterMap() const
{
    return this->registerMap;
}

void Bank::reset()
{
    QMapIterator<int, Register*> it(registerMap);
    while(it.hasNext())
    {
	it.next();
	it.value()->reset();
    }
}
